package com.example.myapplication;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.giant.network.JNIUtils;

import java.io.File;
import java.io.Serializable;
import java.util.List;

/**
 * @author: rongfeng
 * @date: 2021/4/6
 * @description
 */
public class XglActivity extends AppCompatActivity {

    int index = 0;
    int ids[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        next();
    }

    public void next() {
        if (index < ids.length) {
            int lesson_id = ids[index];
            index++;
            long time = System.currentTimeMillis();
            try {
                String json = "{\"lesson_id\":\"" + lesson_id + "\",\"page\":\"1\",\"limit\":\"50\",\"timestamp\":\""+time+"\",\"appversion\":\"1.7.1\",\"apptype\":\"0\",\"channel\":\"xiaomi\",\"brand\":\"nubia\",\"lang\":\"en\",\"model\":\"NX629J\"}";
                JNIUtils jniUtils = new JNIUtils();
                String md5 = jniUtils.stringFromJNI(json, json.length());
                String url = "https://www.giantsapp.com/api/sentence/list?lesson_id="+lesson_id+"&page=1&limit=50&" +
                        "timestamp="+time+"&appversion=1.7.1&apptype=0&channel=xiaomi&brand=nubia&lang=en&model=NX629J&signature=" + md5;
                System.out.println("ssss index " + index);
                getDns(url, lesson_id);
            } catch (Exception e) {

            }

        }
    }


    public void getDns(String url, int lesson_id) {
        String basePath = getPath("sentence");
        DownloadUtil.get().download(url,
                basePath, lesson_id + ".json", new DownloadUtil.OnDownloadListener() {
                    @Override
                    public void onDownloadSuccess(File file) {
                        next();
                        System.out.println("ssss 保存成功 " + file.getAbsolutePath());
                    }

                    @Override
                    public void onDownloading(int progress) {
                    }

                    @Override
                    public void onDownloadFailed(Exception e) {
                        System.out.println("ssss 保存失败 " + index);
                    }
                });
    }


    public String getPath(String filePath) {
        final String path = getFilesDir() + "/" + filePath + "/";
        File file = new File(path);
        if (file.mkdirs()) {
            return path;
        }
        return path;
    }

    public class JsonRootBean implements Serializable {

        private int code;
        private String message;
        private List<Data> data;

        public void setCode(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getMessage() {
            return message;
        }

        public void setData(List<Data> data) {
            this.data = data;
        }

        public List<Data> getData() {
            return data;
        }

    }


    public class Data implements Serializable {

        private int id;
        private int lesson_id;
        private String en_content;
        private String cn_content;
        private double audio_start_time;
        private double en_audio_start_time;
        private int order;

        public void setId(int id) {
            this.id = id;
        }

        public int getId() {
            return id;
        }

        public void setLesson_id(int lesson_id) {
            this.lesson_id = lesson_id;
        }

        public int getLesson_id() {
            return lesson_id;
        }

        public void setEn_content(String en_content) {
            this.en_content = en_content;
        }

        public String getEn_content() {
            return en_content;
        }

        public void setCn_content(String cn_content) {
            this.cn_content = cn_content;
        }

        public String getCn_content() {
            return cn_content;
        }

        public void setAudio_start_time(double audio_start_time) {
            this.audio_start_time = audio_start_time;
        }

        public double getAudio_start_time() {
            return audio_start_time;
        }

        public void setEn_audio_start_time(double en_audio_start_time) {
            this.en_audio_start_time = en_audio_start_time;
        }

        public double getEn_audio_start_time() {
            return en_audio_start_time;
        }

        public void setOrder(int order) {
            this.order = order;
        }

        public int getOrder() {
            return order;
        }

    }
}